package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Api(tags = "店铺相关接口")
@Slf4j
public class ShopController {

    public static final String KEY = "SHOP_STATUS";
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 设置店铺状态
     * @param status 店铺状态，0-关闭，1-开启
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation(value = "设置店铺状态", notes = "设置店铺状态，0-关闭，1-开启")
    public Result setStatus(@PathVariable Integer status){//这里我们使用redis进行存储 因为如果为了一个status创建一张表接受明显不合理
        log.info("设置店铺状态，status={}", status==1?"营业中":"打烊中");
        redisTemplate.opsForValue().set(KEY, status/*==1?"营业中":"打烊中"*/);//保存到redis中
        return Result.success();
    }

    /**
     * 获取店铺状态
     * @return
     */
    @GetMapping("/status")
    @ApiOperation(value = "获取店铺状态", notes = "获取店铺状态，0-关闭，1-开启")
    public Result<Integer> getStatus(){
        Integer status = (Integer) redisTemplate.opsForValue().get(KEY);
        log.info("获取店铺状态，status={}", status==1?"营业中":"打烊中");
        return Result.success(status);
    }



}
